using System;
using System.Text;
using System.Threading.Tasks;
using TianShu.SysParameterSetting.DAL.Abstracts;
using TianShu.SysParameterSetting.DAL.Abstracts.SystemParameterSetting;
using TianShu.SysParameterSetting.Service.Abstracts.SystemParameterSetting.EventDatas;
using TianShu.SysParameterSetting.Service.Abstracts;
using TianShu.SysParameterSetting.Service.Abstracts.Dto;
using TianShu;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.Abp.Uow;
using Volo.Abp.Application.Services;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Application.Dtos;

namespace TianShu.SysParameterSetting.Service
{
    /// <summary>
    /// 系统参数配置服务实现类
    /// </summary>
    public class SystemParameterSettingServiceImpl : CrudAppService<TianShu.SysParameterSetting.DAL.Abstracts.Entities.SystemParameterSetting,  SystemParameterSettingListOutput, Guid, SystemParameterSettingCreateUpdateInput>, ISystemParameterSettingService
    {
        private readonly IDistributedEventBus _distributedEventBus;
        private readonly ISystemParameterSettingDal  _systemParameterSettingDal;
        /// <sumary>
        /// 系统参数配置构造函数
        /// </sumary>
        public SystemParameterSettingServiceImpl(IDistributedEventBus distributedEventBus
            , ISystemParameterSettingDal systemParameterSettingDal
            ,IRepository<DAL.Abstracts.Entities.SystemParameterSetting,Guid> systemParameterSettingRepository):base(systemParameterSettingRepository)
        {
            _distributedEventBus = distributedEventBus;
            _systemParameterSettingDal = systemParameterSettingDal;
        }

        public async Task<T> GetValue<T>(string code)
        {
            return await _systemParameterSettingDal.GetValue<T>(code);
        }

        public override async Task<ResultDto<SystemParameterSettingListOutput>> CreateAsync(SystemParameterSettingCreateUpdateInput dto)
        {
            var codeNameEntity = await Repository.FindAsync(e => (e.Code == dto.Code || e.Name == dto.Name));
            if (codeNameEntity != null)
            {
                return new ResultDto<SystemParameterSettingListOutput>(L[SystemParametrSettingConst.CodeNameExists].Value);
            }
            var result = await base.CreateAsync(dto);
            await _distributedEventBus.PublishAsync(new SystemParameterSettingAddOrUpdateEventData
            {
                Code = dto.Code,
                Value = dto.Value,
            });
            return result;
        }

        public override async Task<ResultDto<SystemParameterSettingListOutput>> UpdateAsync(Guid id, SystemParameterSettingCreateUpdateInput dto)
        {
            var codeNameEntity = await Repository.FindAsync(e => (e.Code == dto.Code || e.Name == dto.Name) && e.Id != id);
            if (codeNameEntity != null)
            {
                return new ResultDto<SystemParameterSettingListOutput>(L[SystemParametrSettingConst.CodeNameExists].Value);
            }
            var result = await base.UpdateAsync(id, dto);

            await _distributedEventBus.PublishAsync(new SystemParameterSettingAddOrUpdateEventData
            {
                Code = dto.Code,
                Value = dto.Value,
            });
            return result;
        }
    }
}
